/*
this program is used to send msg to trigger the ele machine operation
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>

#include <sys/stat.h>
#include <fcntl.h>

#include <linux/can.h>
#include <linux/can/raw.h>

#include "../app/common.h"
#include "../app/msg_def.h"
#include "./data_proc/board_data_proc.h"

#define FILE_NAME "qpcr_ctl.txt"

//#define msgQueueKeyPath "/home/usr/msg/msg.tmp"
//#define FILE_NAME "uim_cmd.txt"

#define ACT_SID(sid) (((sid&0x1f)<<6)+2)
#define ACT_EID(sid,tcw) (((sid&0x60)<<8) + tcw)

// typedef struct _msg_can_frame
// {
//     long type;
//     struct can_frame canFrame;
// }msg_can_frame;

// typedef enum
// {
//    RET_BACK,
//    MOVE,
//    STOP,
//    QUERY,
// }OPCODE;

// typedef struct _ctl_uim
// {
//     unsigned char sid;
//     unsigned char opcode;
//     unsigned char dir;
//     unsigned char reserved;
//     unsigned int position;
//     unsigned int queryParam;
// }ctl_uim;


// typedef struct _msg_ctl_uim
// {
//     long type;
//     ctl_uim ctlFrame;
// }msg_ctl_uim;

int main(int argc, char *argv[])
{
    FILE * fp;
    unsigned char sid = 0;
    unsigned char tcw = 0;
    int flag_file = 0;
    int dlc_f = 0;
    unsigned char data_f[8];

    struct can_frame canFrame;

    msg_ctl_uim ctlFrame;

    memset(&ctlFrame,0,sizeof(msg_ctl_uim));

    //msg_can_frame msgFrame;

    int flagValid = 0;

    if(argc<2)
    {
        fp = fopen(FILE_NAME,"r");
        

    }
    else
    {
        fp = fopen(argv[1],"r");
    }
    if(fp!=NULL)
    {
        char StrLine[256];
        char * tmpPtr = NULL;

        ctlFrame.type = QUEUE_MSG_TYPE_BOARDS;
        ctlFrame.ctlFrame.opcode = MAX_OP_FROM_CENTRE;

        ctlFrame.ctlFrame.un_cmd.qpcrCtlData.paramType = 0xff;
        //ctlFrame.ctlFrame.sid = QPCR_CTRL_BOARD;
        while(!feof(fp))
        {
           memset(StrLine,0,256);
           fgets(StrLine,256,fp);
           if(strstr(StrLine,"sid"))
           {
               tmpPtr = StrLine+4;
               sid = atoi(tmpPtr);
               flag_file = 1;
               ctlFrame.ctlFrame.sid = sid;
               continue;
           }

           if(strstr(StrLine,"op"))
           {
               char * tempPtr = StrLine+3;

               if(strstr(tempPtr,"start"))
               {
                   ctlFrame.ctlFrame.opcode = CENTRE_START;
               }
               else if(strstr(tempPtr,"stop"))
               {
                   ctlFrame.ctlFrame.opcode = CENTRE_STOP;
               }
               else if(strstr(tempPtr,"config"))
               {
                   ctlFrame.ctlFrame.opcode = CENTRE_CONFIG;
               }
               else if(strstr(tempPtr,"read"))
               {
                   ctlFrame.ctlFrame.opcode = CENTRE_READ;
               }

               continue;               
           }

           if(strstr(StrLine,"paramType"))
           {
               char * tempPtr = StrLine+10;

               if(strstr(tempPtr,"kpr"))
               {
                   ctlFrame.ctlFrame.un_cmd.qpcrCtlData.paramType = QPCR_KPR;
               }
               else if(strstr(tempPtr,"kpf"))
               {
                   ctlFrame.ctlFrame.un_cmd.qpcrCtlData.paramType = QPCR_KPF;
               }
               else if(strstr(tempPtr,"hight"))
               {
                   ctlFrame.ctlFrame.un_cmd.qpcrCtlData.paramType = QPCR_HIGHT;
               }               

               continue;               
           }

           if(strstr(StrLine,"valuei"))
           {
                char * tempPtr = StrLine+7;
                ctlFrame.ctlFrame.un_cmd.qpcrCtlData.paramDataI = atoi(tempPtr);
           }

           if(strstr(StrLine,"valuef"))
           {
                char * tempPtr = StrLine+7;
                ctlFrame.ctlFrame.un_cmd.qpcrCtlData.paramDataf = atof(tempPtr);
           } 

           if(strstr(StrLine,"chan"))
           {
                char * tempPtr = StrLine+5;
                ctlFrame.ctlFrame.un_cmd.qpcrCtlData.chanNum = atoi(tempPtr);
           }         

        }
        
    }
    else
    {
        /* code */
        printf("open file failed\n");
    }

    key_t key = ftok(msgQueueKeyPath, 0x1234);
    if(key < 0)
    {
        perror("ftok");
        printf("%s\n",msgQueueKeyPath);
        return -1;
    }

    int msg_id = msgget(key, IPC_CREAT|0600);//IPC_CREAT|0600
    if(msg_id < 0)
    {
        perror("msgget");
        return -1;
    }
    

    if(msgsnd(msg_id, (void*)&ctlFrame, sizeof(ctl_uim), 0) < 0)
    {
        perror("msgsnd");
        return -1;
    }
    else
    {
        /* code */
        printf("msg send success,type is %d,opcode[%d]\n",ctlFrame.type,ctlFrame.ctlFrame.opcode);
    }

    return 0;
} 
/*
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <time.h>

#define TEXT_SIZE 512

struct _msgbuf
{

    long mtype;
    int status;
    char time[20];
    char mtext[TEXT_SIZE];    
};

char *getxtsj()
{
    time_t tv;
    struct tm *tmp;
    static char buf[20];

    tv = time( 0 );

    tmp = localtime(&tv);

    sprintf(buf,"%02d:%02d:%02d",tmp->tm_hour , tmp->tm_min,tmp->tm_sec);
    return buf;
}

int main(int argc, char **argv)

{

    int msqid ;

struct msqid_ds info;

struct _msgbuf buf;

struct _msgbuf buf1;

int flag ;

int sendlength, recvlength ;

int key ;



key = ftok("msg.tmp", 0x01 ) ;

if ( key < 0 )

{

perror("ftok key error") ;

return -1 ;

}



msqid = msgget( key, 0600|IPC_CREAT ) ;

if ( msqid < 0 )

{

 perror("create message queue error") ;
return -1 ;

}



buf.mtype = 1;

buf.status = 9;

strcpy(buf.time, getxtsj()) ;

strcpy(buf.mtext, "happy new year!\n") ;

sendlength = sizeof(struct msgbuf) - sizeof(long) ;

flag = msgsnd( msqid, &buf, sendlength , 0 ) ;

if ( flag < 0 )

{

perror("send message error") ;

return -1 ;

}

buf.mtype = 3 ;

buf.status = 9 ;

strcpy(buf.time, getxtsj()) ;

strcpy(buf.mtext, "good bye!") ;

sendlength = sizeof(struct msgbuf) - sizeof(long) ;

flag = msgsnd( msqid, &buf, sendlength , 0 ) ;

if ( flag < 0 )

{

perror("send message error") ;

return -1 ;

}
system("ipcs -q") ;

return 0 ;

}*/
